package com.gepengjun.springboot.jwt.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class JwtUtil {

    private static final long EXPIRE_TIME = 1000;

    public static String getToken(String userId, String userName, String salt){
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);

        Algorithm algorithm = Algorithm.HMAC256(salt);

        //设置头信息
        Map<String,Object> header = new HashMap<>(2);
        header.put("typ","JWT");
        header.put("alg","HMAC256");

        return JWT.create().withHeader(header)
                .withIssuer("www.gepengjun.com")
                .withIssuedAt(new Date())
                .withNotBefore(new Date())
                .withExpiresAt(date)
                .withJWTId(UUID.randomUUID().toString())
                .withClaim("userId",userId)
                .withClaim("userName",userName)
                .sign(algorithm);
    }

    public static boolean verify(String token, String salt){

        Algorithm algorithm = Algorithm.HMAC256(salt);
        JWTVerifier jwtVerifier = JWT.require(algorithm).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        return true;


    }
}
